package cn.cosmosx.controller.sys;


import cn.cosmosx.base.define.ResultVO;
import cn.cosmosx.base.define.SingleParam;
import cn.cosmosx.base.encrypt.aes.AESUtil;
import cn.cosmosx.base.helper.SessionHelper;
import cn.cosmosx.base.security.entity.SecurityUserVo;
import cn.cosmosx.base.encrypt.jwt.JWTsUtil;
import cn.cosmosx.controller.sys.param.UserSecurityParam;
import cn.cosmosx.service.sys.AuthenticationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author gengzhy
 * @since 2022-10-02 23:52:28
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/sys/user")
public class SysUserController {
    @Autowired
    private AuthenticationService authenticationService;

    @ApiOperation(value = "用户登录")
    @PostMapping("/login")
    public ResultVO<?> login(@Validated @RequestBody UserSecurityParam param) throws IOException {
        authenticationService.login(param.getUsername(), param.getPassword());
        return ResultVO.success();
    }

    @ApiOperation(value = "退出登录")
    @GetMapping("/logout")
    public ResultVO<?> logout() {
        authenticationService.logout();
        return ResultVO.success();
    }

    @ApiOperation(value = "生成token")
    @PostMapping("/jwt")
    public String generateJwt(@RequestBody SingleParam<String> id) {
        return AESUtil.encrypt(JWTsUtil.token(id.getKey()));
    }

    @ApiOperation(value = "获取登录用户")
    @PostMapping("/get")
    public SecurityUserVo getUser() {
        return SessionHelper.getUser();
    }
}
